<template>
  <div class="contain">
    <el-card style="margin: 20px">
      <!-- 提示文案 -->
      <el-alert type="warning" :closable="false" style="margin-bottom: 10px">
        <i class="el-icon-info" />说明：目前只支持学科和关键字条件筛选
      </el-alert>
      <!-- 表单区域，收集信息 -->
      <el-form label-width="100px">
        <!-- 第一行标签 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="学科" style="margin-bottom: 2px">
              <el-select
                v-model="searFrom.subjectID"
                size="small"
                placeholder="请选择学科"
              >
                <el-option
                  v-for="item in SubjectList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- 可搜索 -->
            <el-form-item label="关键字" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.keyword"
                size="small"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="二级目录" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行标签 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <el-form-item label="试题类型" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tags"
                size="small"
                placeholder="请输入企业标签"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="难度" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.shortName"
                size="small"
                placeholder="请输入关键字"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行表单框 -->
        <el-row :gutter="3">
          <el-col :span="6">
            <el-form-item label="题目备注" style="margin-bottom: 2px">
              <el-input
                v-model="searFrom.tags"
                size="small"
                placeholder="请输入题目备注"
              />
            </el-form-item>
          </el-col>
          <!-- 选择城市和区 -->
          <el-col :span="6">
            <el-form-item label="城市" style="margin-bottom: 2px">
              <el-select v-model="province" size="small">
                <el-option
                  v-for="item in Area"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区" style="margin-bottom: 2px">
              <el-select v-model="city" size="small">
                <el-option
                  v-for="item in cityQuery"
                  :key="item.value"
                  :label="item.label"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第四行标签 -->
        <el-row>
          <el-form-item label="企业简称">
            <el-col :span="18">
              <el-input
                v-model.trim="searFrom.state"
                style="width: 200px; margin-right: 15px"
                placeholder="请输入企业简称"
                size="small"
              />

              <el-button size="small" @click="handelMationClean">
                清空
              </el-button>
              <el-button type="primary" size="small" @click="getDatabaseList">
                搜索
              </el-button>
            </el-col>
            <el-col :span="6" style="text-align: right">
              <el-button
                type="success"
                class="right"
                size="small"
                @click="$router.push(`/list/card`)"
              >
                新增试题
              </el-button>
            </el-col>
          </el-form-item>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert type="info" :closable="false">
        <i class="el-icon-info" />共{{ total }}条记录</el-alert
      >
      <!-- 表单区域 -->
      <el-table :data="DatabaseList">
        <el-table-column label="试题编号" prop="number" />
        <el-table-column label="学科" prop="subject" />
        <el-table-column label="目录" prop="catalog" />
        <el-table-column label="题型" prop="questionType">
          <template #default="{ row }">
            <span v-if="row.questionType === '1'">单选</span>
            <span v-if="row.questionType === '2'">多选</span>
            <span v-if="row.questionType === '3'">简答</span>
          </template>
        </el-table-column>
        <el-table-column label="题干" prop="question">
          <template #default="{ row }">
            <div v-html="row.question" />
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addDate" />
        <el-table-column label="难度" prop="difficulty">
          <template #default="{ row }">
            <div v-if="row.difficulty === '1'">
              {{ '简单' }}
            </div>
            <div v-if="row.difficulty === '2'">
              {{ '普通' }}
            </div>
            <div v-if="row.difficulty === '3'">
              {{ '困难' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入人" prop="creator" />
        <el-table-column label="地区" prop="city" />
        <el-table-column label="备注" prop="tags" />
        <el-table-column label="方向" prop="direction" />
        <el-table-column label="企业简称" prop="enterprise" />

        <el-table-column label="操作" width="210" fixed="right">
          <template #default="{ row }">
            <div>
              <!-- 查看详情按钮 -->
              <el-tooltip content="详情" placement="top" effect="dark">
                <el-button
                  type="primary"
                  icon="el-icon-thumb"
                  circle
                  plain
                  @click="DialogShow(row)"
                />
              </el-tooltip>
              <!-- 修改按钮 -->
              <el-tooltip content="修改" placement="top" effect="dark">
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  circle
                  plain
                  @click="$router.push(`/list/card/?obj=${row.id}`)"
                />
              </el-tooltip>
              <!-- 删除按钮 -->
              <el-tooltip content="删除" placement="top" effect="dark">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  circle
                  @click="delCompany(row)"
                />
              </el-tooltip>
              <!-- 精选按钮 -->
              <el-tooltip
                v-if="row.choiceState !== 1"
                content="加入精选"
                placement="top"
                effect="dark"
              >
                <el-button
                  type="success"
                  icon="el-icon-check"
                  circle
                  plain
                  @click="resetCompanyState(row)"
                />
              </el-tooltip>
              <el-tooltip
                v-else
                content="取消精选"
                placement="top"
                effect="dark"
              >
                <el-button
                  type="warning"
                  icon="el-icon-close"
                  circle
                  plain
                  @click="resetCompanyState(row)"
                />
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="text-align: right; margin-top: 10px">
        <el-pagination
          :current-page="searFrom.page"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="searFrom.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 详情组件 -->
    <inFoCmd
      :show-dialog="showDialog"
      :subject-data="subjectData"
      @add-success="getSubjectList"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import {
  getDatabaseListAPI,
  delQuestionAPI,
  featureQuestionAPI
} from '@/api/table'
import { getSubjectListAPI } from '@/api/subject'
// 导入组件
import { citys } from '@/components/MyCity'
import inFoCmd from './components/InfoCom.vue'
export default {
  name: 'Table',
  components: { inFoCmd },
  data() {
    return {
      subjectData: {},
      // 详情组件显示
      showDialog: false,
      Area: [],
      cityQuery: [],
      areaQuery: [],
      province: '',
      city: '',
      area: '',
      // 搜索表单对象
      searFrom: {
        page: 1,
        pagesize: 5,
        keyword: '',
        subjectID: null
      },
      // 题库数据
      DatabaseList: [],
      total: 0,
      editForm: {},
      // 学科列表
      SubjectList: []
    }
  },
  watch: {
    // 监听省级
    province: function (newValue) {
      this.cityQuery = []
      this.shi = ''
      console.log(this.Area)
      this.Area.forEach((item) => {
        if (newValue.label === item.label) {
          this.cityQuery = item.children
          console.log('city', this.cityQuery)
        }
      })
    }
  },
  created() {
    this.getSubjectList()
    this.getDatabaseList()
    this.Area = citys
  },
  methods: {
    // 详情框打开时
    DialogShow(e) {
      // console.log(e)
      this.subjectData = e
      this.showDialog = true
    },
    // 关闭详情框
    closeDialog() {
      this.showDialog = false
    },
    // 获取学科列表
    async getSubjectList() {
      const { data } = await getSubjectListAPI()
      console.log('学科列表', data)
      this.SubjectList = data
    },
    // 精选
    async resetCompanyState(quesData) {
      console.log(quesData)
      try {
        if (quesData.choiceState === 1) {
          await this.$confirm('确定移除精选吗？', '温馨提示', {
            type: 'warning'
          })
          await featureQuestionAPI(quesData.id, 0)
          this.$message.success('移出成功🤡')
        } else {
          await this.$confirm('确定精选该试题吗？', '温馨提示', {
            type: 'warning'
          })
          await featureQuestionAPI(quesData.id, 1)
          this.$message.success('试题精选成功🤡')
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.getDatabaseList()
      }
    },
    // 当条数改变时
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.searFrom.pagesize = val
      this.getDatabaseList()
    },
    // 当页数改变时
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.searFrom.page = val
      this.getDatabaseList()
    },
    // 删除试题
    async delCompany(question) {
      try {
        await this.$confirm('确定删除该试题吗？', '温馨提示', {
          type: 'warning'
        })
        await delQuestionAPI(question)
        this.$message.success('删除试题成功，丁真为你点赞🤪')
        // console.log(resp)
        this.getDatabaseList()
      } catch (error) {
        console.log(error)
      }
    },
    // 清空表单
    handelMationClean() {
      this.searFrom = {}
      this.city = this.province = ''
      this.getDatabaseList()
    },
    // 获取试题列表
    async getDatabaseList() {
      const { data } = await getDatabaseListAPI(this.searFrom)
      // console.log(data)
      this.DatabaseList = data.items
      this.total = data.counts
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}
</style>

